import logging
import time
import  os
import  subprocess
from fengfan_src.conf.env.testConfig import  config
'''
class appium(object):
    def  __init__(self):
        self.logger=logging.getLogger(__name__)

    def  start(self):
        self.logger.info('starting Appium Server')
        currentTime  =time.strftime('%Y%m%d%H%M%S',time.localtime())
        udid         =self.getudid()
        appiumPort   =config['appiumPort']
        bootStrapPort=config['bootStrapPort']
        seldnroidPort=config['seldnroidPort']
        chromiumPort =config['chromiumPort']
        logPath      =os.path.abspath(os.path.join(os.getcwd(),'feng_test_logfile','AS'+currentTime+'.feng_test_logfile'))
        try:
            self.process = subprocess.Popen('appium --prot={} --bootStrap-port={} --selendroid-port={} --chromedriver-port={}'
                                            '--feng_test_logfile={}'.format(appiumPort,bootStrapPort,seldnroidPort,chromiumPort,logPath,udid),
                                            stdout=subprocess.PIPE,
                                            shell=True)
        except Exception as e:
            self.logger.error('start appium sever failed!')
            self.logger.error('errorMsg:{}'.format(e))
        time.sleep(5)
    def stopappium(self):
        self.logger.info('stop appium server!')
        self.process.kill()
        os.system('taskkill /im node.exe /f')
    def getudid(self):
        'adb devices'
        cmd ='adb  devices'
        output=subprocess.Popen(cmd,stdout=subprocess.PIPE,shell=True)   #命令行输入 adb devices
        infoList=output.stdout.read().decode('utf-8').strip('List of  devices attached').split() #读取内存地址内容转化为utf-8格式然后  去掉strip最后split转化为list
        if  len(infoList) !=0:
            deviceList=[]
            for deviceInfo in infoList:
                if  deviceInfo!='device':
                    deviceList.append(deviceInfo)
        return deviceList[0]
'''
